﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Common.DBOper
{



   
     public class Cls_Data
     {
         public int Id;

         public long TestId;

         public int ChannelId;

         public float TotaldB;

         public float HighdB;

         public float LowdB;

         public float VelocityRMS;

         public List<float> _1_3octavedB=new List<float> ();

         public int AccelerationFFTAmpDB_length;

         public List<float> AccelerationFFTAmpDB = new List<float>();

         public int AccelerationTimewave_Length;

         public List<float> AccelerationTimewave = new List<float>();

         public string TestTime;

     

    }


    public  class OperateData
    {
        
        /// <summary>
        /// 添加通道配置信息
        /// </summary>
        /// <param name="clstest">测试类</param>
        /// <param name="filepath">数据库路径</param>
        public int AddData(Dictionary<byte, List<Cls_Data>> datalist)
        {
            int updatecount = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    mydb.BeginTransaction();
                    bool insertisok=true;
                    foreach (var item in datalist.Keys)
                    {
                        if (mydb.ExecuteNonQuery_Data(datalist[item])<0)
                        {
                            insertisok = false;
                            break;
                        }
                    }
                    if (insertisok)
                    {
                        mydb.Commit();
                    }
                    else 
                    {
                        mydb.Rollback();
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    updatecount = -1;
                    Mdl_Class.WriteLog.dbErrLog("AddData(List<Cls_Data> datalist)", ex.ToString());
                }
            }
            return updatecount;
        }


        public Cls_Data GetDataByChannelid(string testid,byte channelId) 
        {
            Cls_Data clsdata = null;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select * from T_Data  where TestId='{0}' and ChannelId={1}", testid, channelId);
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        clsdata = new Cls_Data();
                        clsdata.Id = Convert.ToInt32(mydb.DataReader["ID"]);
                        clsdata.TestId = Convert.ToInt64(mydb.DataReader["TestId"]);
                        clsdata.ChannelId = Convert.ToInt32(mydb.DataReader["ChannelId"].ToString());
                        clsdata.TotaldB = Convert.ToSingle(mydb.DataReader["TotaldB"].ToString());
                        clsdata.HighdB = Convert.ToSingle(mydb.DataReader["HighdB"].ToString());
                        clsdata.LowdB = Convert.ToSingle(mydb.DataReader["LowdB"].ToString());
                        clsdata.VelocityRMS = Convert.ToSingle(mydb.DataReader["VelocityRMS"].ToString());
                        clsdata._1_3octavedB = mydb.BytesToFloat(mydb.DataReader["_1_3octavedB"] as byte[]).ToList();
                        clsdata.AccelerationFFTAmpDB = mydb.BytesToFloat(mydb.DataReader["AccelerationFFTAmpDB"] as byte[]).ToList();
                        clsdata.AccelerationTimewave = mydb.BytesToFloat(mydb.DataReader["AccelerationTimewave"] as byte[]).ToList();
                        clsdata.TestTime = mydb.DataReader["TestTime"].ToString();
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    clsdata = null;
                    Mdl_Class.WriteLog.dbErrLog("GetDataList(int testid,int channelId)", ex.ToString());
                }

                return clsdata;
            }
        }
    }



     
}
